package com.mengjiahui.auduoplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mengjiahui.auduoplayer.R;
import com.mengjiahui.auduoplayer.bean.Music;

import java.util.List;

/**
 * @author Meng
 * @data 2020/5/9
 */
public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.ViewHolder> {
    private Context context;
    private int resourceId;
    private List<Music> musics;
    private OnItemClickListener listener=null;

    public PlayListAdapter(Context context, int resourceId, List<Music> musics) {
        this.context = context;
        this.resourceId = resourceId;
        this.musics = musics;
    }


    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(resourceId,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Music music=musics.get(position);
        holder.imageView.setImageResource(music.getMusicIcon());
        holder.textView.setText(music.getMusicName()+"\n"+music.getAuthor());
        if(listener!=null) {
            holder.itemView.setOnClickListener(v -> {
                listener.onItemClick(position);
            });
        }
    }

    @Override
    public int getItemCount() {
        return musics.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageViewMucisIcon);
            textView=itemView.findViewById(R.id.textViewMusicInfo);
        }
    }

    public interface  OnItemClickListener{
        void onItemClick(int position);
    }
}
